package com.chuangke.common.db.page;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

public class PageUtil {

    /**
     * 从结果集做分页，代替数据库分页
     *
     * @param list
     * @return
     */
    public static <T> IPage<T> findPage(CkPageQuery page, List<T> list) {
        IPage<T> result = page.getPage();

        long totalSize = list.size();
        result.setTotal(totalSize);

        long startNum = (result.getCurrent() - 1) * result.getSize();

        if (startNum >= totalSize) {
            return result;
        }

        long endNum = result.getCurrent() * result.getSize();
        if (endNum >= totalSize) {
            endNum = totalSize;
        }

        result.setRecords(list.subList((int) startNum, (int) endNum));

        return result;
    }
    
    public static  <T> IPage<T> convert(IPage<?> oldPage){
    	IPage<T> newPage = new Page<>() ;
    	newPage.setCurrent(oldPage.getCurrent()) ;
    	newPage.setPages(oldPage.getPages()) ;
    	newPage.setSize(oldPage.getSize()) ;
    	newPage.setTotal(oldPage.getTotal()) ;
    	return newPage ;
    }

}
